<template>
    <div class="pad20">
        <div class="filter-container">
            <!--查询的表单数据-->
            <el-form :inline="true" :model="map" size="mini">
                <el-form-item label="课程名称:">
                    <el-input v-model.trim="map.title"></el-input>
                </el-form-item>
                <el-form-item label="上下架:">
                    <el-select v-model="map.status" class="auto-width" clearable filterable placeholder="上下架" style="width: 85px">
                        <el-option
                                v-for="item in opts.isPutawayList"
                                :key="item.code"
                                :label="item.desc"
                                :value="item.code">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button icon='el-icon-search' type="primary" @click="handleCheck">查询</el-button>
                    <el-button icon='el-icon-refresh' class="filter-item" @click="handleReset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <!--修改的弹出框-->
        <el-dialog
                title="修改"
                :visible.sync="visible"
                @close="closeCllback"
                width="70%">
            <el-form ref="formDataRef" :model="formData" label-width="100px">
                <el-form-item label="课程名称：">
                    <el-input v-model="formData.title"></el-input>
                </el-form-item>
                <el-form-item label="是否免费：">
                    <el-radio-group v-model="formData.price" v-if="formData.price!=0">
                        <el-radio :label="formData.price">收费</el-radio>
                        <el-radio :label="0.00">免费</el-radio>
                    </el-radio-group>
                    <el-radio-group v-model="formData.price" v-if="formData.price==0">
                        <el-radio :label="1">收费</el-radio>
                        <el-radio :label="0.00">免费</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-row v-if="formData.price != 0">
                    <el-col :span="12">
                        <el-form-item label="原价：">
                            <el-input type="text" style="width: 120px" placeholder="请输入价格" v-model="formData.price"></el-input> 元
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="课程简介:">
                    <!--<el-input
                            type="textarea"
                            autosize
                            placeholder="请输入内容"
                            v-model="formData.remark">
                    </el-input>-->
                    <quill-editor class="editor"
                                  v-model="formData.description"
                                  ref="myQuillEditor"
                                  :options="editorOption">
                    </quill-editor>
                </el-form-item>
                <el-row style="margin-top:17px; ">
                    <el-button style="float:right;margin-left:6px;" size="mini" type="danger" plain @click="visible = false">取 消</el-button>
                    <el-button style="float:right" size="mini" type="primary" @click="submitForm">确定</el-button>
                </el-row>
            </el-form>
        </el-dialog>
        <!--审核的弹出框-->
        <el-dialog
                title="课程审核"
                :visible.sync="checkvisible"
                :before-close="closeCllback1"
                width="30%">
            <el-form ref="checkformDataRef" :model="checkformData" label-width="100px">
                <el-form-item label="审核状态：">
                    <el-radio-group v-model="checkformData.checkStatus">
                        <el-radio label="1">通过</el-radio>
                        <el-radio label="0">不通过</el-radio>
                        <el-radio label="2">待审核</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注：">
                    <el-input type="textarea" :rows="3" placeholder="系统默认通过" v-model="checkformData.checkContent"></el-input>
                </el-form-item>
            </el-form>
            <el-row style="margin-top:17px; ">
                <el-button style="float:right;margin-left:6px;" size="mini" type="danger" plain @click="resetCheck">取 消</el-button>
                <el-button style="float:right" size="mini" type="primary" @click="submitCheckForm">确定</el-button>
            </el-row>
        </el-dialog>
        <!--数据表格-->
        <el-table  size="medium" :data="list" stripe border>
            <el-table-column type="index" label="序号" width="50">
            </el-table-column>
            <el-table-column prop="cover" label="课程封面" width="122">
                <template slot-scope="scope">
                    <img :src="scope.row.cover" :alt="scope.row.title" width="100" height="60">
                </template>
            </el-table-column>
            <el-table-column prop="title" label="课程名称" width="180">
            </el-table-column>>
            <el-table-column label="课程分类" align="center" prop="subjectName" width="400px">

            </el-table-column>
            <el-table-column label="价格" prop="price" width="150">
                <template slot-scope="scope">
                    <span v-if="scope.row.price !== 0">原价：{{parseFloat(scope.row.price).toFixed(2)}}<br>优惠价：{{(scope.row.price*0.8).toFixed(2)}}</span>
                    <span v-else>免费</span>
                </template>
            </el-table-column>
            <el-table-column
                    width="150"
                    prop="status"
                    label="上下架"
                    align="center">
                <template slot-scope="scope">
                    <el-switch
                            v-model="scope.row.status"
                            @change="handleChangeIsPutaway(scope.row.id,scope.row.status)"
                            active-value="Draft"
                            inactive-value="Normal"
                            active-color="#ff4949"
                            inactive-color="#13ce66"
                            active-text="下架"
                            inactive-text="上架">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column
                    label="审核状态"
                    prop="checkStatus"
                    width="80"
                    align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.checkStatus === '2'">待审核</span>
                    <span v-if="scope.row.checkStatus === '1'">审核通过</span>
                    <span v-if="scope.row.checkStatus === '0'">审核不通过</span>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button  type="success" @click="handleEdit(scope.row)" size="mini">修改</el-button>
                    <el-button  type="primary" @click="handleCheckCourse(scope.row)" size="mini">审核</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                style="float: right;margin-top: 20px; margin-bottom: 22px"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :page-size="page.pageSize"
                :page-sizes="[5, 8, 10, 15, 20, 30]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.totalCount">
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "courseCheck",
        data() {
            return {
                editorOption:{},
                editor: {},
                dialogVisible: false,
                editVisible: false,
                visible: false,
                checkvisible:false,
                map: {},
                formData: {},
                checkformData:{},
                list: [],
                opts: {
                    isFreeList: [{
                        code: 1,
                        desc:"收费"
                    },{
                        code: 0,
                        desc:"免费"
                    }],
                    isPutawayList: [{
                        code: "Normal",
                        desc:"上架"
                    },{
                        code: "Draft",
                        desc:"下架"
                    }]
                },
                page: {
                    beginPageIndex: 1,
                    currentPage: 1,
                    endPageIndex: 8,
                    pageCurrent: 1,
                    pageSize: 5,
                    totalCount: 0,
                    totalPage: 0
                }
            }
        },
        created() {
            this.searchCourse()
        },
        methods: {
            // 课程分类分页列表接口
            searchCourse(){
                var that =this
                this.$http.post(`/core/checkCourse/findCourseCheck/${this.page.pageCurrent}/${this.page.pageSize}`,this.map).then(function (resp) {
                    if (resp.data.code==200){
                        that.list = resp.data.result.records
                        that.page.totalCount = resp.data.result.total
                        that.page.pageSize = resp.data.result.size
                    }
                })
            },
            //提交修改的表单
            submitForm(){
                var that = this
                this.$http.post(`/core/course/updateCourse`,this.formData).then(function (resp) {
                    if (resp.data.code==200){
                        that.$message.success(resp.data.msg);
                        that.visible = false
                    }else {
                        that.$message.error(resp.data.msg);
                        that.visible = false
                    }
                })
            },
            //提交审核的表单
            submitCheckForm(){
                /*if (this.checkformData.checkContent === ""){
                    this.checkformData.checkContent  = "系统默认通过";
                }*/
                var that = this
                this.$http.post(`/core/checkCourse/updateCourseCheck`,this.checkformData).then(function (resp) {
                    if (resp.data.code==200){
                        that.$message.success(resp.data.msg);
                        that.checkvisible = false
                        that.searchCourse()
                    }else {
                        that.$message.error(resp.data.msg);
                        that.checkvisible = false
                        that.searchCourse()
                    }
                })
            },
            //审核取消后清空
            resetCheck(){
                this.checkvisible = false
                this.checkformData.checkContent = ""
                this.reload()
            },
            //查询
            handleCheck() {
                this.searchCourse()
            },
            // 重置查询条件
            handleReset() {
                this.reload()
            },
            handleSizeChange(val) {
                this.page.pageSize = val
                this.searchCourse()
            },
            handleCurrentChange(val) {
                this.page.pageCurrent = val
                this.searchCourse()
            },
            // 刷新当前页面
            reload() {
                this.map = {}
                this.formData = {}
                this.searchCourse()
            },
            // 关闭编辑弹窗回调
            closeCllback() {
                this.visible = false;
                this.$refs.formDataRef.resetFields();
                this.reload()
            },
            //关闭审核弹框回调
            closeCllback1(){
                this.checkvisible = false;
                this.$refs.checkformDataRef.resetFields();
                this.reload()
            },
            // 修改上下架状态
            handleChangeIsPutaway(id, status) {
                const title = {"Draft": '下架', "Normal": '上架'}
                this.$confirm(`确定要${title[status]}吗?`, {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.changeIsPutaway(id, status)
                    //this.reload()
                }).catch(() => {
                    this.reload()
                })
            }
            ,
            // 请求更新上下架方法
            changeIsPutaway(id, status) {
                var that =this
                this.$http.post(`/core/course/changeStatus`, {"id":id,"status":status}).then(function (resp) {
                    if (resp.data.code==200){
                        that.$message.success(resp.data.msg);
                    }
                    that.reload()
                })
            },
            // 修改弹窗
            handleEdit(row) {
                this.formData = row
                this.visible = true
            },
            //审核课程
            handleCheckCourse(row){
                this.checkformData = row
                this.checkvisible = true
            },
        }

    }
</script>

<style scoped>

</style>
